<?php
// error_reporting(E_ALL);

require_once "helpers.php";

class Db
{
    private $_is_log_queries    = false;
    private $_is_time_queries   = false;
    private $_row_count         = 0;
    private $_affected_count    = 0;
    private $_is_echo_sql       = 0;
    
    public function Db($islog = false, $istime = false)
    {
        $this->_is_log_queries = $islog ;
        $this->_is_time_queries = $istime ;

        $server     = DB_HOST;
        $user       = DB_USER;
        $password   = DB_PASSWORD;
        $dbname     = DB_NAME;

        if(!  @mysql_connect($server, $user, $password) ) {
            $ierr = mysql_errno() ;
            switch( $ierr ) {
                case 2013:
                    die( "The database server is unavailable." ) ;
                    
                default:
                    die( "Database connection error $ierr: ". mysql_error() ) ;
            }
        }
        if( ! mysql_select_db($dbname) )
            die( "Database selection error: ".mysql_error() ) ;
    }

    private function _SqlResult($sql)
    {
        if( $this->_is_echo_sql )
            echo PrettySql($sql) ;
        if($this->_is_log_queries)
            $this->Log($sql);
        $result = mysql_query($sql)
              or die("[_SqlResult] oops: <br>"
                    . PrettySql($sql)
                    . "<br>" 
                    . mysql_error());
        if(!is_bool($result)) {
            $this->_row_count = mysql_num_rows($result);
        }
        else {
            $this->_affected_count = mysql_affected_rows();
        }
        return $result;
    }

    public function SqlRows($sql)
    {
        $rows = array();
        $result = $this->_SqlResult($sql);
        if(is_bool($result)){
            $this->_affected_count = mysql_affected_rows();
        }
        else {
            $this->_row_count = mysql_num_rows($result);
            if($this->RowCount() > 0) {
                while($row = mysql_fetch_array($result)) {
                    $rows[] = $row;
                }
            }
        }
        mysql_free_result($result);
        return $rows;
    }

    public function SqlObjects($sql)
    {
        $objects = array();
        $result = $this->_SqlResult($sql);
        if(is_bool($result)) {
            $this->_affected_count = mysql_affected_rows();
        }
        else {
            $this->_row_count = mysql_num_rows($result);
        }
        if($this->RowCount() > 0) {
            while($object = mysql_fetch_object($result)) {
                $objects[] = $object;
            }
            mysql_free_result($result);
            return $objects;
        }
        else {
            return null;
        }
    }

    public function SqlOneObject($sql)
    {
        $result = $this->_SqlResult($sql);
        if(is_bool($result)) {
            $this->_affected_count = mysql_affected_rows();
        }
        else {
            $this->_row_count = mysql_num_rows($result);
        }
        if($this->RowCount() > 0) {
            $obj = mysql_fetch_object($result);
        }
        else {
            $obj = null;
        }
        mysql_free_result($result);
        return $obj;
    }
    
    public function SqlOneRow($sql)
    {
        $result = $this->_SqlResult($sql);
        if(is_bool($result)) {
            $this->_affected_count = mysql_affected_rows();
        }
        else {
            $this->_row_count = mysql_num_rows($result);
        }
        if($this->RowCount() > 0) {
            $row = mysql_fetch_array($result);
        }
        else {
            $row = null;
        }
        mysql_free_result($result);
        return $row;
    }

    public function SqlOneValue($sql, $debug = false)
    {
        $result = $this->_SqlResult($sql);
        if(is_bool($result)) {
            $this->_affected_count = mysql_affected_rows();
        }
        else {
            $this->_row_count = mysql_num_rows($result);
        }
        if($this->RowCount() > 0) {
            $row = mysql_fetch_array($result);
            $val = $row[0];
        }
        else {
            $val = null;
        }
        mysql_free_result($result);
        return $val ;
    }

    public function SqlExecute($sql)
    {
        if($this->_is_log_queries)
            $this->Log($sql);
        if($this->_is_time_queries) {
            global $marktime;
            $marktime = microtime(true);
        }
        mysql_query($sql);
        if($this->_is_log_queries) {
            $echotime = sprintf(" Build Time: %5.3f ", microtime(true) - $marktime);
            $this->Log("Execution time: $echotime");
        }
        return mysql_affected_rows();
    }

    public function RowCount() {
        return $this->_row_count;
    }

    public function SqlExists($sql)
    {
        $result = $this->SqlOneRow($sql);
        return $this->RowCount();
    }

    public function AffectedCount() {
        return $this->_affected_count;
    }

    public function Log($text)
    {
        echo htmlComment( $text ) ;
        $sql = "INSERT INTO Log (LogText)
                VALUES ('".mysql_real_escape_string($text)."')";
        mysql_query($sql)
              or die("[_SqlResult] oops: <br>$sql<br>" . mysql_error());
    }

    public function SetTimeQueries()
    {
        $this->_is_time_queries = true;
    }

    public function ClearTimeQueries()
    {
        $this->_is_time_queries = false;
    }

    public function SetLogQueries()
    {
        $this->_is_log_queries = true;
    }

    public function ClearLogQueries()
    {
        $this->_is_log_queries = false;
    }
}

?>
